<?php
/**
 * @file
 * Contains \Drupal\gdlisnet\Controller\CollectionController.
 */

namespace Drupal\gdlisnet\Controller;

use Drupal\gdlisnet\CollectionInterface;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Controller routines for CollectionController routes.
 */
class CollectionController extends ControllerBase {
	private static function languageIdToName($langId) {
		$langNames=[
			'ace' => '亚齐语', 
			'ach' => '阿乔利语', 
			'ada' => '阿当梅语', 
			'afh' => '阿弗里希利语', 
			'afr' => '南非荷兰语', 
			'afa' => '其它亚非语系', 
			'aka' => '阿坎语', 
			'akk' => '阿卡德语', 
			'alb' => '阿尔巴尼亚语', 
			'ale' => '阿留申群岛之土语', 
			'alg' => '其它阿尔贡语系', 
			'tut' => '其它阿尔泰语系', 
			'amh' => '阿姆哈拉语', 
			'apa' => '阿帕切语', 
			'ara' => '阿拉伯语', 
			'arc' => '阿拉米语', 
			'arp' => '阿拉帕霍语', 
			'arw' => '阿拉瓦克语', 
			'arm' => '亚美尼亚语', 
			'art' => '其它人工语言', 
			'asm' => '阿萨姆语', 
			'ath' => '其它阿撒巴斯卡语系', 
			'aus' => '澳大利亚语', 
			'map' => '其它马来亚玻里尼西亚语系', 
			'ava' => '阿法语', 
			'ave' => '阿维斯塔语', 
			'awa' => '阿瓦乔语', 
			'aym' => '艾马拉语', 
			'aze' => '阿塞拜疆语', 
			'ban' => '巴里语', 
			'bat' => '波罗的海地区之语言', 
			'bal' => '俾路支语', 
			'bam' => '班巴拉语', 
			'bai' => '巴米累克语', 
			'bad' => '班达语', 
			'bas' => '巴萨语', 
			'bak' => '巴什基尔语', 
			'baq' => '巴斯克语', 
			'bej' => '别札语', 
			'bel' => '白俄罗斯语', 
			'bem' => '别姆巴语', 
			'ben' => '孟加拉语', 
			'ber' => '北非—回教土族之语言', 
			'bho' => '博杰普尔语', 
			'bra' => '布拉杰语', 
			'bre' => '布尔吞语', 
			'bug' => '布吉语', 
			'bul' => '保加利亚语', 
			'bur' => '缅甸语', 
			'cad' => '卡多语', 
			'car' => '巴勒比语', 
			'cat' => '加泰隆语', 
			'cau' => '其它高加索语系', 
			'ceb' => '宿务语', 
			'cel' => '其它凯尔特语系', 
			'cai' => '其它中美印第安语系', 
			'chg' => '查加语', 
			'cha' => '查莫罗语', 
			'che' => '车臣语', 
			'chr' => '彻罗基语', 
			'chy' => '切延内语', 
			'chb' => '契布卡语', 
			'chi' => '汉语', 
			'chn' => '契努克语', 
			'cho' => '乔克托语', 
			'chu' => '古代俄语（宗教斯拉夫语）', 
			'chv' => '楚瓦什语', 
			'cop' => '科普特语', 
			'cor' => '科尼什语', 
			'cre' => '克里族语', 
			'mus' => '摩斯科格语', 
			'crp' => '克里奥尔语和皮钦语', 
			'cpe' => '不纯粹之英国方言', 
			'cpf' => '不纯粹之法国方言', 
			'cpp' => '不纯粹之葡国方言', 
			'cus' => '其它库施特语系', 
			'cze' => '捷克语', 
			'dak' => '达科他语', 
			'dan' => '丹麦语', 
			'del' => '特拉瓦印第安人语', 
			'din' => '丁卡语', 
			'doi' => '多格来语', 
			'dra' => '其它德拉维语系', 
			'dua' => '都阿拉语', 
			'dut' => '荷兰语', 
			'dum' => '中古荷兰语', 
			'dyu' => '迪尤拉语', 
			'efi' => '艾非克语', 
			'egy' => '埃及语', 
			'eka' => '艾卡朱克语', 
			'elx' => '艾拉米特语', 
			'eng' => '英语', 
			'enm' => '中古英语', 
			'ang' => '古代英语', 
			'epo' => '世界语', 
			'est' => '爱沙尼亚语', 
			'gez' => '埃塞俄比亚语', 
			'ewe' => '幽语', 
			'fan' => '芳格语', 
			'fat' => '芳蒂语', 
			'fao' => '法罗语', 
			'fij' => '斐济语', 
			'fin' => '芬兰语', 
			'fiu' => '芬匈语（其它）', 
			'fon' => '丰语', 
			'fre' => '法语', 
			'frm' => '中古法语', 
			'fro' => '古法语', 
			'fry' => '费里斯语', 
			'ful' => '富拉语', 
			'gaa' => '加语', 
			'gla' => '盖尔语', 
			'lug' => '卢干达语', 
			'gay' => '迦约语', 
			'geo' => '格鲁吉亚语', 
			'ger' => '德语', 
			'gmh' => '中古高地德语', 
			'goh' => '古代高地德语', 
			'gem' => '其它德语语系', 
			'gil' => '吉尔伯特斯语', 
			'gon' => '岗德语', 
			'got' => '哥达语', 
			'grb' => '格列博语', 
			'grc' => '古希腊语', 
			'gre' => '近代希腊语', 
			'grn' => '瓜拉尼语', 
			'guj' => '古吉拉特语', 
			'hai' => '海达语', 
			'hau' => '豪萨语', 
			'haw' => '夏威夷语', 
			'heb' => '希伯来语', 
			'her' => '赫雷罗语', 
			'hil' => '希利盖农语', 
			'him' => '赫马查利语', 
			'hin' => '印地语', 
			'hup' => '胡帕语', 
			'hun' => '匈牙利语', 
			'iba' => '伊班语', 
			'ice' => '冰岛语', 
			'ibo' => '伊格博语', 
			'ijo' => '伊乔语', 
			'ilo' => '伊洛干诺语', 
			'inc' => '印度语（其它）', 
			'ine' => '其它印欧语系', 
			'ind' => '印尼语', 
			'ina' => '国际辅助语', 
			'ira' => '其它伊朗语系', 
			'gle' => '爱尔兰语', 
			'iro' => '伊洛郭伊费语', 
			'ita' => '意大利语', 
			'jpn' => '日语', 
			'jav' => '爪哇语', 
			'jrb' => '犹太—阿拉伯语系', 
			'jpr' => '犹太—波斯语系', 
			'kab' => '卡比尔语', 
			'kac' => '卡琴语', 
			'kam' => '卡姆巴语', 
			'kan' => '坎纳达语', 
			'kau' => '卡努里语', 
			'kaa' => '卡拉卡尔帕克语', 
			'kar' => '喀伦语', 
			'kas' => '克什米尔语', 
			'kaz' => '哈萨克语', 
			'kha' => '卡西语', 
			'khm' => '柬埔寨语', 
			'khi' => '其它科伊桑语', 
			'kho' => '和田语', 
			'kik' => '吉库尤语', 
			'kin' => '卢旺达语', 
			'kon' => '刚果语', 
			'kok' => '刚卡尼语', 
			'kor' => '朝语', 
			'kpe' => '克佩列语', 
			'kro' => '克鲁语', 
			'kur' => '库尔德语', 
			'kru' => '库鲁克语', 
			'kut' => '库特内语', 
			'kir' => '柯尔克孜语', 
			'lad' => '拉迪诺语', 
			'lah' => '拉亨达语', 
			'lam' => '兰巴语', 
			'lao' => '老挝语', 
			'lat' => '拉丁语', 
			'lav' => '拉托维亚语', 
			'lit' => '立陶宛语', 
			'lub' => '鲁巴—加丹加语', 
			'lui' => '路易塞诺语', 
			'lun' => '隆达语', 
			'mac' => '马其顿语', 
			'mad' => '马都拉语', 
			'mag' => '马加伊语', 
			'mai' => '迈蒂利语', 
			'mak' => '望加锡语', 
			'mlg' => '马拉加斯语', 
			'may' => '马来语', 
			'mal' => '马拉维拉姆语', 
			'mlt' => '马尔他语', 
			'man' => '曼丁哥语', 
			'mni' => '曼尼普里', 
			'mno' => '马诺博污', 
			'glv' => '人岛语', 
			'mao' => '毛利语', 
			'mar' => '马拉蒂语', 
			'mah' => '马歇尔语', 
			'mwr' => '马尔尼里语', 
			'mas' => '萨伊语', 
			'myn' => '玛雅语系', 
			'men' => '门迪语', 
			'mic' => '米克马克语', 
			'min' => '米南卡保语', 
			'mis' => '各种不同语言', 
			'moh' => '摩霍克语', 
			'mol' => '摩尔达维亚语', 
			'mon' => '蒙古语', 
			'mul' => '多种语言', 
			'mun' => '其它蒙达语', 
			'nav' => '纳瓦霍语', 
			'ndo' => '恩东加语', 
			'nep' => '尼泊尔语', 
			'new' => '尼瓦尔语', 
			'nic' => '其它尼日尔刚果语系', 
			'niu' => '纽埃语', 
			'nai' => '其它北美印第安语系', 
			'nso' => '北索托语', 
			'nor' => '挪威语', 
			'nub' => '努比亚语', 
			'nym' => '尼亚姆韦齐语', 
			'nya' => '尼昂加语', 
			'nyo' => '尼约罗语族', 
			'oji' => '奥季布瓦语', 
			'ori' => '奥里亚语', 
			'orm' => '奥罗英语', 
			'osa' => '奥萨哲语', 
			'oss' => '奥塞提语', 
			'oto' => '奥托米语系', 
			'pal' => '帕拉维语', 
			'pau' => '帕劳语', 
			'pli' => '帕利语', 
			'pam' => '邦板牙语', 
			'pag' => '邦阿西南语', 
			'pan' => '旁遮普语', 
			'pap' => '帕皮亚内托语', 
			'paa' => '其它巴布亚—澳洲语系', 
			'per' => '波斯语', 
			'phi' => '其它菲律宾语系', 
			'pol' => '波兰语', 
			'pon' => '波纳佩语', 
			'por' => '葡萄牙语', 
			'pra' => '印度古代及中世纪之中部及北部方言', 
			'pro' => '普罗文斯语', 
			'pus' => '普什图语', 
			'que' => '盖丘亚语', 
			'roh' => '罗曼斯方言', 
			'raj' => '拉贾斯坦语', 
			'rar' => '拉罗汤加语', 
			'roa' => '其它拉丁语系', 
			'rum' => '罗马尼亚语', 
			'rom' => '吉普赛语', 
			'run' => '隆迪语', 
			'rus' => '俄语', 
			'sal' => '萨利什语', 
			'sam' => '萨玛利亚语', 
			'smo' => '萨摩亚语', 
			'sad' => '散达维语', 
			'sag' => '桑戈语', 
			'san' => '梵语', 
			'sco' => '苏格兰语', 
			'sel' => '塞尔库普语', 
			'sem' => '其它闪族语系', 
			'scc' => '塞尔维亚—克罗地亚语（基里尔字母之一）', 
			'scr' => '塞尔维亚—克罗地亚语（罗马字母）', 
			'srr' => '谢列尔语', 
			'shn' => '掸语', 
			'sna' => '绍纳语', 
			'sid' => '悉达摩语', 
			'snd' => '信德语', 
			'sin' => '僧加罗语', 
			'sit' => '其它汉藏语系', 
			'sio' => '苏语诸语言', 
			'sla' => '其它斯拉夫语系', 
			'slo' => '斯洛伐克语', 
			'slv' => '斯洛文尼亚语', 
			'sog' => '索格迪亚语', 
			'som' => '索马里语', 
			'son' => '桑海语', 
			'wen' => '索布诸语言', 
			'sot' => '索托语', 
			'sai' => '其它南美印第安语系', 
			'spa' => '西班牙语', 
			'suk' => '苏库马语', 
			'sux' => '苏马语', 
			'sus' => '苏苏语', 
			'swa' => '斯瓦西里语', 
			'ssw' => '斯威士语', 
			'swe' => '瑞典语', 
			'syr' => '叙利亚语', 
			'tgl' => '他加禄语', 
			'tah' => '塔西提语', 
			'tgk' => '塔吉克语', 
			'tam' => '泰米尔语', 
			'tat' => '塔塔尔语', 
			'tel' => '泰卢固语', 
			'tem' => '提姆语', 
			'ter' => '泰雷诺语', 
			'tha' => '泰语', 
			'tib' => '藏语', 
			'tig' => '提格雷语', 
			'tir' => '底格里语', 
			'tiv' => '蒂夫语', 
			'tli' => '特林吉特语', 
			'tog' => '汤加语（尼亚萨地区）', 
			'ton' => '汤加语（汤加岛）', 
			'chk' => '特鲁克语', 
			'tsi' => '蒂姆西亚语', 
			'tso' => '聪加语', 
			'tsn' => '茨瓦纳语', 
			'tum' => '通布卡语', 
			'tur' => '土耳其语', 
			'ota' => '奥托曼土耳其语', 
			'tuk' => '土库曼语', 
			'twi' => '特威语', 
			'uga' => '乌加里特语', 
			'uig' => '唯吾尔语', 
			'ukr' => '乌克兰语', 
			'umb' => '莱都（姆崩杜语）', 
			'und' => '未定语种', 
			'urd' => '乌尔都语', 
			'uzb' => '乌兹别克语', 
			'vai' => '瓦伊语', 
			'ven' => '文达语', 
			'vie' => '越南语', 
			'vot' => '沃提克语', 
			'wal' => '瓦拉莫语', 
			'was' => '瓦绍语', 
			'wel' => '威尔斯语', 
			'wol' => '沃洛夫语', 
			'xho' => '科萨语', 
			'yao' => '瑶族语', 
			'yap' => '雅浦语', 
			'yid' => '依地语', 
			'yor' => '约鲁巴语', 
			'zap' => '萨波蒂克语', 
			'zen' => '泽纳加语', 
			'zul' => '祖鲁语', 
			'zun' => '祖尼语'
			];
		return $langNames[$langId];
	}
	
	public function CollectionEntityPage(CollectionInterface $collection) {
		$build=[];
		$rows=[];
		if($collection->get('MARC类型')==1) {
			$fields=['责任者', '语种', '出版者', '标准编码', '索书号', '册数'];
			foreach($fields as $key=>$value) {
				$v=$collection->get($value);
				if(!is_null($v)) {
					if($value=='语种') $v=self::languageIdToName($v);
					$rows[]=[
						['data'=>is_numeric($key)?$value:$key, 'header'=>TRUE, 'nowrap'=>TRUE],
						$v,
					];
				}
			}
		} else {
			$rows[]=['FMT', 'MU'];
			$rows[]=['LDR', '0000cjm'];
			foreach($collection->get('marc')->getFields() as $field) {
				if($field->isControlField()) {
					$rows[]=array($field->getTag(), ['data'=>$field->getData(), 'colspan'=>2]);
				} else {
					$subfields_text='';
					foreach($field->getSubfields() as $code=>$sub_field) {
						$subfields_text.='|'.$code.str_replace('[sound resource]', '[sound recording]', $sub_field->getData());
					}
					$ind1=$field->getIndicator(1)===false||$field->getIndicator(1)==' '?" ":$field->getIndicator(1);
					$ind2=$field->getIndicator(2)===false||$field->getIndicator(2)==' '?" ":$field->getIndicator(2);
					if($field->getTag()=='007' && substr($subfields_text,0,2)=='|a')
						$subfields_text=substr($subfields_text,2);
					$rows[]=array($field->getTag().$ind1.$ind2,$subfields_text);
				}
			}
		}
		$build['data']=[
			'#theme' => 'table',
			'#header' => ['', ''],
			'#rows' => $rows,
		];
		//var_dump($collection);
		return $build;
	}
	
	public function CollectionEntityPageTitle(CollectionInterface $collection) {
		return $collection->get('题名2');
	}
}

?>
